'use strict';

module.exports = app => {
    class TeachingService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        async teaching() {
            const result = await this.ctx.fetch(this.app.urls('getnotice'), {
                method: 'get',
                dataType: 'json',
            });
            let data = result.data;

            this.app.logger.info(data);

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async ann(params){
            const result = await this.ctx.fetch(this.app.urls('getnoticedetail'),{
                data:{
                    id:params.id,
                },
                method:'get',
                dataType:'json',
            });
            let data = result.data;

            this.app.logger.info(data);

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        //获取教师备课
        async prepare(){
            const result = await this.ctx.fetch(this.app.urls('getprepare'),{
                method:'get',
                dataType:'json',
            });
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }
            var date = new Date();

            for(let i=0;i<data.data['list'].length;i++){
                var time = new Date(data.data['list'][i]['createDate']);
                data.data['list'][i]['createMonth'] = time.getMonth()+1;
                if(data.data['list'][i]['createMonth'] == date.getMonth()+1){
                    data.data['list'][i]['createMonth'] = '本月';
                }else{
                    data.data['list'][i]['createMonth'] = time.getMonth()+1+'月';
                }
            }

            console.log(data.data);


            var dd= data.data['list'];
            var bb={}
            bb.currentTime="本月";
            bb.data=dd;

            var month=['01','02','03','04','05','06','07','08','09','10','11','12']

            month.reverse();



            var arry = [];

             var currentMonth = date.getMonth()+1;
             for(var j=0;j<month.length;j++){
                 var a1 = {};
                a1.currentTime=month[j];
                a1.status = '';
                var a2 = new Array();
                 for(var i=0;i<data.data['list'].length;i++){
                     if(data.data['list'][i]['createDate'].substring(5,7) == month[j]){
                         a2.push(data.data['list'][i]);
                         a1.currentTime = data.data['list'][i]['createMonth'];
                         a1.status = 1;
                     }
                 }

                 a1.data=a2;
                 arry.push(a1);
             }




            data.data['list'] = arry;

            // data.data['list'].forEach(function(item, index) {
            //     if (!obj[item.createMonth]) {
            //         obj[item.createMonth] = [];
            //     }
            //     obj[item.createMonth].push(item)
            // });
            console.log(data.data['list']);
            return data.data;
        }




        //获取教师备课详情
        async preparedetail(e){
            const result = await this.ctx.fetch(this.app.urls('getpreparedetail'),{
                method:'get',
                dataType:'json',
                data:{
                    id:e.id
                }
            });
            let data = result.data;

            this.app.logger.info(data);

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }




        /**
         *  添加请假条
         */
        async setLeave(e) {
            let url = this.app.urls('setleave');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    type:e.type,
                    startDate:e.startDate,
                    startPart:e.startPart,
                    endDate:e.endDate,
                    endPart:e.endPart,
                    content:e.content,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }




        /*
        获取最新的一条请假条
         */
        async getLeave() {
            let url = this.app.urls('getLeave');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /*
            获取请假条记录
        */
        async getLeaveList() {
            let url = this.app.urls('getLeaveList');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /*
        获取请假条详情
         */

        async getLeaveDetail(params) {
            let url = this.app.urls('getLeaveDetail');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    id:params.id,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /*
            撤销请假条
         */

        async delLeave(params) {
            let url = this.app.urls('delLeave');
            this.app.logger.info(params.leaveId);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    leaveId:params.leaveId,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }



        /*
            修改请假条
        */

        async editLeave(e) {
            let url = this.app.urls('editLeave');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    id:e.id,
                    type:e.type,
                    startDate:e.startDate,
                    startPart:e.startPart,
                    endDate:e.endDate,
                    endPart:e.endPart,
                    content:e.content,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


    }

    return TeachingService;
};
